#include "StdAfx.h"
#include "CMusicListStatu.h"
#include "CListBox.h"
#include "CMusicPlayStatu.h"
#include "MediaPlayer.h"


CMusicListStatu::CMusicListStatu(WCHAR* name):IStatu(name)
{
	m_bFirst=true;
	m_nIndexPos=1;
}

CMusicListStatu::~CMusicListStatu(void)
{
}

bool CMusicListStatu::EvaluateClickEvent(const WCHAR* elementName)
{
	if (wcscmp(elementName,L"musiclistup") == 0)
		return upclick();
	else if (wcscmp(elementName,L"musiclistdown") == 0)
		return downclick();
	else if (wcscmp(elementName,L"music_btn_home") == 0)
		return homeclick();


	return false;
}
bool CMusicListStatu::EvaluateSliderMoveEvent(const WCHAR* elementName)
{
	
	return false;

}

bool CMusicListStatu::EvaluateDBClickEvent(const WCHAR* elementName)
{
	if (wcscmp(elementName,L"listview_music") == 0)
		return playclick();
               
	
	return false;
}
void CMusicListStatu::SetCurDir(wstring strDir)
{
	CListBox* p = (CListBox*)GRAPHICMGR.GetElement(L"listview_music");
	if (!p)
		return ;

		p->SetCurDir(strDir.c_str());
}
wstring CMusicListStatu::GetSelItemString()
{
	CListBox* p = (CListBox*)GRAPHICMGR.GetElement(L"listview_music");
	if (!p)
		return L"";

	return p->GetItemData(p->GetSelectedItem());
}
bool CMusicListStatu::EnterStatu()
{
	CListBox* p = (CListBox*)GRAPHICMGR.GetElement(L"listview_music");
	if (!p)
		return false;


	if (m_bFirst)
	{

		m_veFilter.push_back(L"mp3");
		m_veFilter.push_back(L"wma");
	
		p->SetExtName(&m_veFilter);

		p->SetCurDir(g_strMediaDir.c_str());
	//	int a=p->GetListTotalItem()-p->GetListTotalFile();
	//	p->SetSelectedItem(p->GetListTotalItem()-p->GetListTotalFile());

		//CMusicPlayStatu* pPlaying = (CMusicPlayStatu*)GRAPHICMGR.GetStatu(L"music_play");


	}
	else
	{
		int nIndex = -1;
		p->ShowList();
		nIndex=CEMediaPlayer::GetPlayerInstance()->GetPlayIndex();
		if(p->GetListTotalItem()-p->GetListTotalFile()>0)
		{
		
			p->SetSelectedItem(nIndex+p->GetListTotalFile());

		}

	}
	return true;

}
bool CMusicListStatu::ExitStatu()
{
	KillTimer(GRAPHICMGR.GetWnd(),SDCARD_TIMEER);
	return true;

}
void	CMusicListStatu::LineUp()
{

	CListBox *pList = (CListBox*)GRAPHICMGR.GetElement(L"listview_music");
	pList->LineUp();
	pList->SetSelectedItem(pList->GetCurLine()-1);
	GRAPHICMGR.SetUpdateElements(L"listview_music",L"none");
	return ;
}
void	CMusicListStatu::LineDown()
{
	CListBox *pList = (CListBox*)GRAPHICMGR.GetElement(L"listview_music");
	pList->LineDown();
     pList->SetSelectedItem(pList->GetCurLine()-1);
	GRAPHICMGR.SetUpdateElements(L"listview_music",L"none");
	return ;
}
bool CMusicListStatu::upclick()
{
	CListBox *pList = (CListBox*)GRAPHICMGR.GetElement(L"listview_music");
	m_nIndexPos--;
	if(m_nIndexPos < 1)
	{
		m_nIndexPos = 1;
	}
	pList->SetCurPage(m_nIndexPos);
	GRAPHICMGR.SetUpdateElements(L"listview_music",L"none");
	return true;
}
bool CMusicListStatu::downclick()
{
	CListBox *pList = (CListBox*)GRAPHICMGR.GetElement(L"listview_music");
	m_nIndexPos++;
	if(m_nIndexPos >= pList->GetTotalPage())
	{
		m_nIndexPos = pList->GetTotalPage();
	}

	pList->SetCurPage(m_nIndexPos);
	GRAPHICMGR.SetUpdateElements(L"listview_music",L"none");
	return true;
}
bool CMusicListStatu::playclick()
{

	{
		CMusicPlayStatu* pPlaying = (CMusicPlayStatu*)GRAPHICMGR.GetStatu(L"musicplay");
		if (pPlaying)
		{

			pPlaying->Stop();
			CStaticText* ptxtplaytime = (CStaticText*)GRAPHICMGR.GetElement(L"txtplaytime");
			if (ptxtplaytime)
				ptxtplaytime->setvalue(L"00:00");
			pPlaying->StartPlay();
		}

		GRAPHICMGR.PostChange(L"musicplay");
		m_bFirst=false;
	}
return true;
}

bool CMusicListStatu::homeclick()
{
	if (m_bFirst)
	{
		//::PostMessage(GRAPHICMGR.GetWnd(),WM_DESTROY,0,0);
		PostMessage(GRAPHICMGR.GetWnd(), WM_CLOSE, 0, 0);
		return true;
	}
	else
	{
		GRAPHICMGR.BackState();
		if( !CEMediaPlayer::GetPlayerInstance()->IsRunning() )
		{
			if(CEMediaPlayer::GetPlayerInstance()->Play())
			{

				CButton * pBtn1 = (CButton*)GRAPHICMGR.GetElement(L"MusicPlay");
				if (pBtn1)
					pBtn1->m_bVisible = false;
				CButton * pBtn = (CButton*)GRAPHICMGR.GetElement(L"Musicpause");
				if (pBtn)
					pBtn->m_bVisible = true;

			

				SetTimer(GRAPHICMGR.GetWnd(),CHECK_TIMER,300,UpdateUITimerProc);
				//KillTimer(GRAPHICMGR.GetWnd(),CHECK_TIMER);
			}
		}
	}
	return false;
}
